import { createRouter, createWebHistory } from 'vue-router'
const Home = () => import( '../views/home/Home')
const Category = () => import( '../views/category/Category')
const Detail = () => import( '../views/detail/Detail')
const Profile = () => import( '../views/profile/Profile')
const ShopCart = () => import( '../views/shopcart/ShopCart')
const Register = () => import( '../views/profile/Register')
const Login = () => import( '../views/profile/Login')

import store from '../store'

import { Notify, Toast } from 'vant';

const routes = [
  {
    path: '',
    name: 'DefaultHome',
    component: Home,
    meta: {
      title: '图书商城'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      title: '图书商城'
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: Category,
    meta: {
      title: '图书商城-商品分类'
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: Detail,
    meta: {
      title: '图书商城-商品详情'
    }
  },
  {
    path: '/shopCart',
    name: 'ShopCart',
    component: ShopCart,
    meta: {
      title: '图书商城-购物车',
      isAuthRequired:true
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: {
      title: '图书商城-个人中心',
      isAuthRequired:true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: {
      title: '图书商城-用户注册'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '图书商城-用户登录'
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next)=>{
  //如果没有登录，在这里Login
  if(to.meta.isAuthRequired && store.state.user.isLogin === false) {
    Notify('你还没登录，请先登录');
    return next('/login')
  }else {
    next();
  }
  document.title = to.meta.title;
})

export default router
